<template>
    <page-header-wrapper @back="onBack">
        <a-card :bordered="false">
            <s-table :loading="loading" :cols="cols" :data="data" :actions="acts"
                     :operators="operators" :selection="selection"/>
            <SForm :visible="modal.visible2" :loading="modal.loading" @ok="signOk"
                   @cancel="modal.visible2 = modal.loading = false"/>
        </a-card>
    </page-header-wrapper>
</template>

<script>
    import STable from '@/components/Table/PagerTable'
    import API from "@/api"
    import SForm from '@/components/Forms/SignForm'
    import ChangeStatus from '@/utils/statusUtils'

    export default {
        name: "interviewDetails",
        components: {
            STable,
            SForm
        },
        data() {
           const selection = this.$route.params.back.indexOf('/system/') >= 0
           const operators = selection ? [{type: 'default', icon: 'team', title: '面试', selection: true, func: this.interview,
               items: [{key: 0, title: '取消面试'}, {key: 2, title: '面试通过'}, {key: -1, title: '简历不通过'},
                   {key: -2, title: '面试不通过'}, {key: -3, title: '缺席面试'}]},
               {type: 'default', icon: 'file-done', title: '签约', selection: true, func: this.sign,
                   items: [{key: 1, title: '安排签约'}, {key: 2, title: '签约完成'},
                       {key: -1, title: '拒绝录用'}, {key: -2, title: '对方拒签'}]},
               {type: 'default', icon: 'arrow-down', title: '下载', selection: true, func: this.downloads}] : null
           return {
               loading: true,
               data: [],
               cols: [{title: '姓名', key: 'name'}, {title: '电话', key: 'phone'}, {title: '身份证号', key: 'idNumber'}
               , {title: '学历', key: 'education'}, {title: '籍贯', key: 'nativePlace'}],
               acts: [{func: this.download, title: '下载'}],
               operators,
               selection,
               modal: {
                   visible2: false,
                   loading: false,
                   keys: null
               }
           }
        },
        mounted() {
            this.query()
        },
        methods: {
            onBack() {
                this.$router.push({path: this.$route.params.back})
            },
            query() {
                this.loading = true
                API.Resume.ListByInterviewId({id: this.$route.params.id}).then((res) => {
                    if (res) {
                        if (res.data) res.data.forEach(item => item.key = item.id)
                        this.data = res.data
                    }
                }).finally(() => this.loading = false)
            },
            interview(opt, keys) {
                if (opt === 0) ChangeStatus(this.callback, 1, opt, keys, this.$route.params.id)
                else ChangeStatus(this.callback, 1, opt, keys)
            },
            sign(opt, keys) {
                if (opt === 1) {
                    this.modal.keys = keys
                    this.modal.visible2 = true
                }
                else ChangeStatus(this.callback, 0, opt, keys)
            },
            signOk(time) {
                ChangeStatus(this.callback, 0, 1, this.modal, time)
            },
            callback(keys) {
                if (keys) this.data = this.data.filter(item => keys.indexOf(item.key) < 0)
            },
            download(record) {
                if (record.attachments) {
                    window.open('/' + record.attachments, "_blank")
                    return
                }
                this.$notification.warn({message: '提示', description: '该简历未上传原文件'})
            },
            downloads(keys) {
                let total = 0
                let downloaded = 0
                this.data.forEach(item => {
                    keys.forEach(key => {
                        if (key === item.key) {
                            total++
                            if (item.attachments) {
                                window.open('/' + item.attachments, "_blank")
                                downloaded++
                            }
                        }
                    })
                })
                this.$notification.warn({message: '提示', description: '已选: ' + total + ' 已下载: ' + downloaded})
            }
        }
    }
</script>
